package com.woniuxy.dr_mall.controller;


import com.woniuxy.dr_mall.entity.DrAddress;
import com.woniuxy.dr_mall.entity.DrUser;
import com.woniuxy.dr_mall.entity.MyResponseEntity;
import com.woniuxy.dr_mall.global.Global;
import com.woniuxy.dr_mall.global.ResponseStateCode;
import com.woniuxy.dr_mall.service.IDrAddressService;
import com.woniuxy.dr_mall.service.IDrRegionService;
import com.woniuxy.dr_mall.utils.Audience;
import com.woniuxy.dr_mall.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张哲轩
 * @since 2021-08-11
 */
@RestController
@RequestMapping("/dr_mall/drAddress")
public class DrAddressController {
    @Autowired
    private IDrAddressService iDrAddressService;
    @Autowired
    private IDrRegionService iDrRegionService;

    @Autowired
    private Audience audience;


    @GetMapping("del/{id}")
    public MyResponseEntity del(@PathVariable Integer id){
        iDrAddressService.delById(id);
        return MyResponseEntity.SUCCESS;
    }

    @GetMapping("getAddress/{id}")
    public MyResponseEntity getAddress(@PathVariable Integer id){
        DrAddress address = iDrAddressService.getById(id);
        address.setProvinceId(iDrRegionService.getIdByName(address.getProvince()));
        address.setCityId(iDrRegionService.getIdByName(address.getCity()));
        address.setDistrictId(iDrRegionService.getIdByName(address.getDistrict()));
        System.out.println(address.toString());

        MyResponseEntity myResponseEntity = new MyResponseEntity();
        myResponseEntity.setCode(ResponseStateCode.SUCCESS);
        myResponseEntity.setData(address);

        return myResponseEntity;
    }


    @PostMapping("setDefault")
    public MyResponseEntity setDefault(@RequestBody DrAddress drAddress,@RequestHeader("token") String token){
        int userId= JwtUtil.getId(token,audience.getBase64Secret());
        Integer id = drAddress.getId();
        iDrAddressService.editStatusIsNByUserId(userId);
        iDrAddressService.editStatusIsYById(id);

        return MyResponseEntity.SUCCESS;
    }

    @PostMapping("add")
    public MyResponseEntity add(@RequestBody DrAddress drAddress,@RequestHeader("token") String token){
        //获取userId
        //drAddress.setUserId(((DrUser)session.getAttribute("user")).getId());
        int id1= JwtUtil.getId(token,audience.getBase64Secret());

        drAddress.setUserId(id1);
        List<DrAddress> addressList = iDrAddressService.getAllByUserId(drAddress.getUserId());
        int size = addressList.size();
        if (size >= 5){
            //606
            return MyResponseEntity.VALUE_EXCEEDS;
        }

        Integer provinceId = Integer.parseInt(drAddress.getProvince());
        Integer cityId = Integer.parseInt(drAddress.getCity());

        if (drAddress.getDistrict() != ""){
            Integer districtId = Integer.parseInt(drAddress.getDistrict());
            drAddress.setDistrict(iDrRegionService.getById(districtId).getName());
        }
        drAddress.setProvince(iDrRegionService.getById(provinceId).getName());
        drAddress.setCity(iDrRegionService.getById(cityId).getName());
        Boolean status = Boolean.valueOf(drAddress.getStatus());

        if (status){
            drAddress.setStatus(Global.NORMAL);
        }else {
            drAddress.setStatus(Global.UNNORMAL);
        }
        iDrAddressService.addAddress(drAddress);
        if (drAddress.getStatus().equals('y')){
            Integer userId = drAddress.getUserId();
            Integer id = drAddress.getId();
            iDrAddressService.editStatusIsNByUserId(userId);
            iDrAddressService.editStatusIsYById(id);
        }

        return MyResponseEntity.SUCCESS;
    }

    /**
     * 地址修改
     * @param drAddress
     * @return
     */
    @PostMapping("edit")
    public MyResponseEntity edit(@RequestBody DrAddress drAddress){
        if (drAddress.getDistrict() != ""){
            Integer districtId = Integer.parseInt(drAddress.getDistrict());
            drAddress.setDistrict(iDrRegionService.getById(drAddress.getDistrict()).getName());
        }
        drAddress.setProvince(iDrRegionService.getById(drAddress.getProvince()).getName());
        drAddress.setCity(iDrRegionService.getById(drAddress.getCity()).getName());

        Boolean status = Boolean.valueOf(drAddress.getStatus());
        if (status){
            drAddress.setStatus(Global.NORMAL);
        }else {
            drAddress.setStatus(Global.UNNORMAL);
        }
        if (drAddress.getStatus().equals('y')){
            Integer userId = drAddress.getUserId();
            Integer id = drAddress.getId();
            iDrAddressService.editStatusIsNByUserId(userId);

        }
        iDrAddressService.editAddress(drAddress);
        return MyResponseEntity.SUCCESS;
    }
    /**
     * 根据用户id获取地址列表   缺用户id
     * @return
     */
    @GetMapping("getList")
    public MyResponseEntity getList(@RequestHeader("token") String token){
        int id= JwtUtil.getId(token,audience.getBase64Secret());
//        List<DrAddress> all = iDrAddressService.getAllByUserId(((DrUser)session.getAttribute("user")).getId());


        List<DrAddress> all = iDrAddressService.getAllByUserId(id);
        MyResponseEntity myResponseEntity = new MyResponseEntity<List<DrAddress>>();
        myResponseEntity.setCode(ResponseStateCode.SUCCESS);
        myResponseEntity.setMsg("查询成功");
        myResponseEntity.setData(all);
        return myResponseEntity;
    }

    @GetMapping("test")
    public MyResponseEntity test(HttpSession session ,@RequestHeader("token") String token){
            return MyResponseEntity.SUCCESS;
    }
}

